package com.wdzl.controller;

import com.wdzl.pojo.User;
import com.wdzl.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Controller
@SessionAttributes({"user"})
@RequestMapping("user")
public class LoginController {
    private final UserService userService;

    public LoginController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("login")
    public String login(User user, Model model, HttpServletRequest request){
        User select = userService.select (user);
        System.out.println (select);
        if (select==null){
            model.addAttribute ("msg","账号或密码错误");
            return "index";
        }else {
            model.addAttribute ("user",select);
        return "redirect:/percent/list";}
    }
    @GetMapping("register")
    public String register(User user){
        return "register";
    }
    @PostMapping("register")
    public String registers(@Valid User user, BindingResult bindingResult){
if (bindingResult.hasErrors ())
{return "register";}
else {
    userService.insert (user);
   return "index";
}
    }

}
